﻿using MediatR;
using Production_Get.API.DTO;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.MaterialManagement
{
    public class ItemInfoScretenHandler : IRequestHandler<ItemInfoScretenCommend, Result>
    {
        private readonly IBaseRepositoryGet<ItemInfoDtos> ItemInfoDtos;

        public ItemInfoScretenHandler(IBaseRepositoryGet<ItemInfoDtos> itemInfoDtos)
        {
            ItemInfoDtos = itemInfoDtos;
        }

        public async Task<Result> Handle(ItemInfoScretenCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            string sql = "SELECT * FROM ItemInfo a INNER JOIN ItemServe c on a.ItemServeId=c.ItemServeId JOIN ItemType d on a.ItemTypeId=d.ItemTypeId JOIN ItemUnit e on a.ItemUnitId=e.ItemUnitId JOIN SpecsUnit f on a.SpecsUnitId=f.SpecsUnitId";
            ItemInfoDtos come = new ItemInfoDtos();
            var list = ItemInfoDtos.GetAll(sql).ToList();

            if (!string.IsNullOrEmpty(request.ItemCode))
            {
                list = list.Where(x => x.ItemCode.Contains(request.ItemCode)).ToList();
            }
            
            if (!string.IsNullOrEmpty(request.ItemName))
            {
                list = list.Where(x => x.ItemName.Contains(request.ItemName)).ToList();
            }
            
            if (request.ItemServeId > 0)
            {
                list = list.Where(x => x.ItemServeId == request.ItemServeId).ToList();
            }

            if (request.ItemTypeId > 0)
            {
                list = list.Where(x => x.ItemTypeId == request.ItemTypeId).ToList();
            }

            if (list != null)
            {
                result.Code = (int)CommonErrorCodes.Success;
                result.Data = list;
                result.Msg = "显示成功";
            }
            else
            {
                result.Msg = "显示失败";
            }
            return result;
        }
    }
}
